<?php
class ClusterManager {

	private $open = 1;
	private $study = 0;
	private $match = 0;
	private $dialogue = 0;
	private $respond = 1;
	private $filter = 0;
	private $bak = 0;
	private $acvideo = 1;
	private $math = 0;
	private $autointo = 0;
	private $config = '1000100100';
	private $message = "";
	private $cp = null;
	/**
	 * 构造函数
	 */
	function ClusterManager() {
		if($this->cp==null) {
			$this->cp = new ClusterParam();
		}
		$v = $this->cp->getParamValue('PARAM_CONFIG',true);
		if($v) { 
			$this->config = $v;
		}
		$this->open =     (int)$this->getValue(1);
		$this->study =    (int)$this->getValue(2);
		$this->match =    (int)$this->getValue(3);
		$this->dialogue = (int)$this->getValue(4);
		$this->respond =  (int)$this->getValue(5);
		$this->filter =   (int)$this->getValue(6);
		$this->bak =      (int)$this->getValue(7);
		$this->acvideo =  (int)$this->getValue(8);
		$this->math =     (int)$this->getValue(9);
		$this->autointo = (int)$this->getValue(10);
	}

	public function run() {
		$p = '/@(\S+)\s*/';
		preg_match($p, trim($_POST[Message]), $m);
		if ($m[1] == "群管理") {
			$this->showManager();
		} else
			if ("$m[1]" == "manager") {
				$this->addManager();
			} else {
				$this->changeConfig($m[1]);
			}
	}

	private function changeConfig($key) {
		if($key=="filter" ||$key=="autointo") {
			//$api = new Api("IsClusterAdmin");
			$lv = (int) $_POST[RobotAdmin];
	    	if($lv<1) {
	    		$this->message ="索妮娅还不是本群的管理员,还无法开启这个功能呢-。-";
	    		return ;
	    	}	
		}
		$a = $this-> $key == 1 ? 0 : 1;
		$this->$key = $a ;
		$value = ($this->open).''.$this->study."".$this->match."".$this->dialogue."".$this->respond."".$this->filter."".$this->bak."".$this->acvideo."".$this->math."".$this->autointo;
		$this->cp->saveParamValue('PARAM_CONFIG',$value);
		if ($a == 0) {
			$this->message = "关闭成功";
		} else {
			$this->message = "开启成功";
		}
	}

	private function showManager() {
		$this->message = "欢迎进入qq群管理设置中心\n" .
		"当前模式：\n" .
		"1.服务状态：  \t" . ($this->open == 0 ? "关闭" : "开启") . "\t@open       \t开启/关闭 机器人在群内的服务\n" .
		"2.学习模式：  \t" . ($this->study == 0 ? "关闭" : "开启") ."\t@study      \t开启/关闭 机器人学习\n" . 
		"3.学习词库：  \t" . ($this->match == 0 ? "私有" : "共享") . "\t@match      \t开启/关闭 词库共享\n".
		"5.根据词库应答：\t" . ($this->respond == 0 ? "关闭" : "开启") ."\t@respond     \t开启/关闭 根据词库应答功能\n" . 
		"6.关键字踢人：\t" . ($this->filter == 0 ? "关闭" : "开启") . "\t@filter     \t开启/关闭 关键字踢人功能\n" .
		"7.群聊天记录备份：\t" . ($this->bak == 0 ? "关闭" : "开启") . "\t@bak     \t开启/关闭 群记录备份\n" .
		"8.acfun视频检测： \t" . ($this->acvideo == 0 ? "关闭" : "开启") ."\t@acvideo   \t开启/关闭   自动检测A站视频信息\n" . 
		"9.自动批准入群： \t" . ($this->autointo == 0 ? "关闭" : "开启") ."\t@autointo   \t开启/关闭 自动同意加群申请\n" . 
		"10.计算表达式： \t" . ($this->math == 0 ? "关闭" : "开启") . "\t@math   \t开启/关闭   自动检测并计算数学表达式\n" .	
		"@blacklist  \t\t黑名单功能~！\n" .
		"@addfilter  \t\t过滤规则配置~！\n" .
		"@param        \t高级参数设置~！\n" .
		"@manager    \t添加普通群员为管理员";
	}

	private function addManager() {
		
		$auth = new Authority(true);
		if (!($auth->adminLevel > 0) && ($auth->clusterLevel < 2) ) {
			$this->message = "只有群主才可以的哦~！";
			return;
		}
		$p = '/@manager[\s]+((<-)|(->))([0-9]{4,15})[\s]*([0-9]{0,1})/si';
		preg_match($p, trim($_POST[Message]), $m);
		if ($m[4] == "1004634672") {
			$this->message = "在这里把索尼娅加为管理没有任何意义的。。- -";
			return;
		}

		if ("<-" == $m[1]) {
			if (!$m[4]) {
				$this->message = "无效的qq号码~！- -";
				return;
			}
			$lv = 1;
			if ($auth->adminLevel > 0 && $m[5]) {
				$lv = 2;
			}
			$ret = @ DB :: GetQueryResult("select * from td_cluster_admin where qq='{$m[4]}' and clusternum='{$_POST[ClusterNum]}'", true); //配置参数初始化
			if (count($ret) != 0) {
				$this->message = "已经添加过了哦,仔细看下吧~！喵、、";
				return;
			}
			$arr = array (
				"ClusterNum" => $_POST[ClusterNum],
				"qq" => $m[4],
				"lv" => $lv,
				"addtime" => date("Y-m-d H:i:s", (time() + 8 * 3600))
			);
			@ DB :: SaveTableRow('td_cluster_admin', $arr);
			$this->message = "添加管理员成功,该用户在群内将享有管理员命令的权限~！";

		} else
			if ("->" == $m[3]) {
				if (!$m[4]) {
					$this->message = "无效的qq号码~！- -";
					return;
				}
				$ret = @ DB :: GetQueryResult("select * from td_cluster_admin where qq='{$m[4]}' and clusternum='{$_POST[ClusterNum]}'", true); //配置参数初始化
				if (count($ret) == 0) {
					$this->message = "这个人还没有被添加过哦,仔细看下吧~！喵、、";
					return;
				}
				@ DB :: Query("delete from  td_cluster_admin  where qq='{$m[4]}' and clusternum='{$_POST[ClusterNum]}'");
				$this->message = "移除成功~！该用户如果不是管理员，将无法使用管理员命令~！";

			} else {
				$ret2 = @ DB :: GetQueryResult("select * from td_cluster_admin where   clusternum='{$_POST[ClusterNum]}'", false); //配置参数初始化
				$mess = "";
				$count = count($ret2);
				if ($count != 0) {
					$mess .= "本群额外管理共有{$count}人,分别为：\n";
					foreach ($ret2 as $key => $v) {
						$mess .= "qq号码：{$v[qq]} \n";
					}
				} else {
					$mess .= "本群还未添加额外管理员\n";
				}
				$mess = $mess . "\n" .
				"使用方法：\n" .
				"@manager <-qq\t\t添加一个管理员\n" .
				"@manager ->qq\t\t移除一个管理员\n";
				$this->message = $mess;

			}

	}
	
	private function getValue($index) {
		$v = substr($this->config,$index-1,1);
		return $v;
	}
	private function __get($property_name) {
		return $this-> $property_name;
	}

	private function __set($property_name, $value) {
		$this->$property_name = $value;
	}

}
?>